#include "StdAfx.h"
#include "JCheckMalware.h"
#include "scanapi.h"
#include "scanerrors.h"

//#define LICENSEKEY L"01AA05098BA297E52F32"
#define LICENSEKEY L"2E3BAA419B6DFB8C3ED1"
static CThreatScanner* Scanner = NULL;

/*
 action value applied for an infected file

 SCAN_ACTION_NONE			1	Do nothing
 SCAN_ACTION_ABORT			2	Abort scanning this object.
 SCAN_ACTION_DISINFECT		3	Try to disinfect the object, and if it not succeeds, try to delete the object.
 SCAN_ACTION_RENAME		4	Rename the file.
 SCAN_ACTION_DELETE			5	Delete the file.
 SCAN_ACTION_QUAR_COPY		6	Copy the file to quarantine.
 SCAN_ACTION_QUAR_MOVE		7	Move the file to quarantine.
 SCAN_ACTION_DISINFECT_ONLY	10	Try only to disinfect the object
*/

void OnObjectScanComplete2(ScanCbkParams *params, int *pnScanAction, void *context)
{
//	*pnScanAction = SCAN_ACTION_DISINFECT_ONLY;
	*pnScanAction = SCAN_ACTION_NONE;
}

JCheckMalware::JCheckMalware(void)
{
}


JCheckMalware::~JCheckMalware(void)
{
	if (Scanner)
	{
		ThreatScanner_DestroyInstance(Scanner);
		Scanner = NULL;
	}

	ThreatScanner_Uninitialize();

}

bool JCheckMalware::Check(WCHAR* wPath, bool *pbInfected)
{
	INT scanStatus;
	INT status = 0;
	INT threatType;
	const CHAR_T * threatInfo;

	if(wPath == NULL || pbInfected == NULL)
	{
		return false;
	}

	if(Scanner == NULL)
	{
		if (!Init())
			return false;
	}

	status = ThreatScanner_ScanObject(Scanner, OBJECT_TYPE_FILE, wPath, FALSE, &scanStatus, &threatType, &threatInfo, 0,  NULL);
	if (status != SCANAPI_SUCCESS)
		return false;

	if (scanStatus == OBJECT_STATUS_INFECTED || scanStatus == OBJECT_STATUS_SUSPICIOUS)
	{
		*pbInfected = TRUE;
		return true;
	}
	*pbInfected = FALSE;
	return true;
}


bool JCheckMalware::Init()
{
	const INT iPathLen = 256;
	INT status = 0;
	WCHAR wPath[iPathLen] = {0,};

	Scanner = NULL;

	GetModuleFileNameW(NULL, wPath, iPathLen);

	WCHAR* wEnd = wcsrchr(wPath, L'\\');
	if (wEnd == NULL)
		return false;

	wEnd[0] = 0;

	//swprintf(wPath, L"%s\\Engine", wPath);

	status = ThreatScanner_Initialize(wPath, LICENSEKEY);
	if (status != SCANAPI_SUCCESS)
		return false;

	status = ThreatScanner_CreateInstance(&Scanner);
	if (status != SCANAPI_SUCCESS)
	{
		ThreatScanner_Uninitialize();
		return false;
	}

	ThreatScanner_SetIntOption(Scanner, _optScanArchives, 1);
	ThreatScanner_SetScanCallback2(Scanner, OnObjectScanComplete2, NULL);
	return true;
}

